package com.dlc.felear.xiaoensale.activity;

import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;

import com.dlc.felear.xiaoensale.R;
import com.dlc.felear.xiaoensale.adapter.FoodMaterialControlAdapter;
import com.dlc.felear.xiaoensale.constant.Constant;
import com.dlc.felear.xiaoensale.customView.LoadingDialog;
import com.dlc.felear.xiaoensale.dao.CookingMenuDao;
import com.dlc.felear.xiaoensale.dao.MenuFoodMaterialDao;
import com.dlc.felear.xiaoensale.deviceUtil.TopbarHelper;
import com.dlc.felear.xiaoensale.entity.MenuFoodMaterial;
import com.dlc.felear.xiaoensale.helper.CookingMenuMangerHelper;
import com.dlc.felear.xiaoensale.utils.NumToStringUtil;
import com.itdlc.android.library.base.BaseActivity;
import com.itdlc.android.library.listener.TextWatcherAdapter;

import java.util.List;

import butterknife.BindView;

/**
 * 食材管理列表
 */
public class ManagerFoodMatActivity extends BaseActivity {

    private static final String TAG="EmnuControlActivity";
    private TopbarHelper topbarHelper;
    @BindView(R.id.lv)
    GridView lv;
    @BindView(R.id.et_search)
    EditText etSeach;
    private MenuFoodMaterialDao foodMaterialDao;
    private FoodMaterialControlAdapter adapter;
    private int size=0;
    private LoadingDialog loadingDialog;

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_food_m_c;
    }

    @Override
    protected void initData() {
        foodMaterialDao=new MenuFoodMaterialDao(this);
        topbarHelper=new TopbarHelper(this);
        topbarHelper.setcon(R.drawable.arrow_left);
        topbarHelper.setTitle("食材管理列表");
        topbarHelper.setNavActivity(LoadDataActivity.class);
        adapter=new FoodMaterialControlAdapter(this);
        lv.setAdapter(adapter);
        etSeach.addTextChangedListener(new TextWatcherAdapter() {
            @Override
            public void afterTextChanged(Editable s) {
                adapter.setItems(CookingMenuMangerHelper.getInstance(ManagerFoodMatActivity.this,
                        new CookingMenuDao(ManagerFoodMatActivity.this)).getStdMenuFoodMaterials(""));
            }
        });
        initData1();
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                handleItem(position);
            }
        });
    }

    private void handleItem(int position) {
        MenuFoodMaterial foodMaterial=adapter.getItem(position);
        if(TextUtils.equals(foodMaterial.getFoodName(),"全部删除")){
            if(loadingDialog==null){
                loadingDialog=new LoadingDialog(this,"正在删除");
            }
            loadingDialog.show();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        List<MenuFoodMaterial> tempList=foodMaterialDao.queryForAll((Constant.FOODS_TYPE_1));
                        int size=tempList.size();
                        int current=0;
                        for (MenuFoodMaterial foodMaterial1:tempList){
                            foodMaterialDao.delete(foodMaterial1);
                            current++;
                            Log.e(TAG,"删除进度:"+(current/size)*100);
                            showMsg("删除进度:"+NumToStringUtil.spitDouble((current/size)*100,2)+"%");
                        }
                    }catch (Exception e){
                        Log.e(TAG,"删除:"+e.getMessage());
                    }
                }
            }).start();
            showLongToast("正在删除....");
            return;
        }
    }

    private void showMsg(final String percent){
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                loadingDialog.setMsg(percent);
                if(percent.contains("100")){
                    adapter.setItems(null);
                }
            }
        });
    }


    protected void initData1() {
//        List<MenuFoodMaterial> data=foodMaterialDao.queryForAll((Constant.FOODS_TYPE_1));
//        if(data!=null){
//            MenuFoodMaterial menuFoodMaterial=new MenuFoodMaterial();
//            menuFoodMaterial.setFoodName("全部删除");
//            data.add(0,menuFoodMaterial);
//        }
        adapter.setItems(CookingMenuMangerHelper.getInstance(this,new CookingMenuDao(this)).getStdMenuFoodMaterials(""));
    }


}